package com.xh3140.android.widget.utils;

import android.graphics.Paint;
import android.graphics.Rect;

import androidx.annotation.NonNull;

@SuppressWarnings("unused")
public class PaintUtils {
    private static final Rect BOUNDS = new Rect();

    /**
     * 禁止实例化
     */
    private PaintUtils() {
        throw new AssertionError("This class prohibits instantiation.");
    }

    /**
     * 获取文本宽度 px
     */
    public static int getTextWidth(@NonNull Paint paint, @NonNull String text) {
        return (int) Math.ceil(paint.measureText(text));
    }

    /**
     * 获取字体推荐高度 px
     */
    public static int getFontHeight(@NonNull Paint paint) {
        return (int) Math.ceil(paint.getFontMetrics().descent - paint.getFontMetrics().ascent);
    }

    /**
     * 获取字体最大高度 px
     */
    public static int getFontMaximumHeight(@NonNull Paint paint) {
        return (int) Math.ceil(paint.getFontMetrics().bottom - paint.getFontMetrics().top);
    }

    /**
     * 获取文本区域宽度 px
     */
    public static int getTextBoundsWidth(@NonNull Paint paint, @NonNull String text) {
        paint.getTextBounds(text, 0, text.length(), BOUNDS);
        return BOUNDS.width();
    }

    /**
     * 获取文本区域高度 px
     */
    public static int getTextBoundsHeight(@NonNull Paint paint, @NonNull String text) {
        paint.getTextBounds(text, 0, text.length(), BOUNDS);
        return BOUNDS.height();
    }
}
